﻿using System.Management.Automation;
using DotNet.DevOps.Tasks.AssemblyInfo;
using DotNet.DevOps.Tasks.Util;

namespace DotNet.DevOps.Tasks.Cmdlets
{
    /// <summary>
    /// Set and/or auto-updates Assembly info entries.
    /// Can autoincrement the version/fileversion number by 1 if activated.
    /// Can autoupdate the copyright year.
    /// 
    /// Default behaviour:
    /// * Adds entry if missing.
    /// * Updates copyright year.
    /// 
    /// </summary>
    /// <example>
    /// import-module .\DotNet.DevOps.Tasks.dll
    /// 
    /// Update-AssemblyInfo -Property @{ Files="AssemblyInfo.cs" 
    ///                                  AssemblyFileVersion="$(BUILD_NUMBER)" 
    ///                                  AssemblyVersion="1.0.0.0"  
    ///                                  AssemblyCopyright="Copyright © Master Garrett 2011" 
    ///                                  AutoUpdateCopyrightYear="false" 
    ///                                  AutoIncrementLastVersionNumber="true"
    ///                                }
    /// </example>
    [Cmdlet(VerbsData.Update, "AssemblyInfo", ConfirmImpact = ConfirmImpact.Low, SupportsShouldProcess = false, SupportsTransactions = false)]
    public class Update_AssemblyInfo : Cmdlet, IAssemblyInfo
    {
        /// <summary>
        /// The file(s) to update.
        /// </summary>
        [Parameter(Mandatory = true,  Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Files { get; set; }

        /// <summary>
        /// Value of the AssemblyDescription entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyDescription { get; set; }
        /// <summary>
        /// Value of the AssemblyCompany entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 2, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyCompany { get; set; }
        /// <summary>
        /// Value of the AssemblyConfiguration entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 3, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyConfiguration { get; set; }
        /// <summary>
        /// Value of the AssemblyCulture entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 4, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyCulture { get; set; }
        /// <summary>
        /// Value of the AssemblyProduct entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 5, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyProduct { get; set; }
        /// <summary>
        /// Value of the AssemblyTitle entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 6, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyTitle { get; set; }
        /// <summary>
        /// Value of the AssemblyTrademark entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 7, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyTrademark { get; set; }
        /// <summary>
        /// Value of the AssemblyCopyright entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 8, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyCopyright { get; set; }
        /// <summary>
        /// Value of the AssemblyFileVersion entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 9, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyFileVersion { get; set; }
        /// <summary>
        /// Value of the AssemblyVersion entry.
        /// </summary>
        [Parameter(Mandatory = false, Position = 10, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string AssemblyVersion { get; set; }

        /// <summary>
        /// Default true, set to false if you want the build to do nothing if an entry is missing. A warning will be displayed however.
        /// </summary>
        [Parameter(Mandatory = false, Position = 11, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public bool AddIfMissing { get; set; }
        /// <summary>
        /// Default true,  will replace any year in the Copyright string to the current year.
        /// </summary>
        [Parameter(Mandatory = false, Position = 12, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public bool AutoUpdateCopyrightYear { get; set; }
        /// <summary>
        /// Default false, will increment last segment of the existing version number by one.
        /// </summary>
        [Parameter(Mandatory = false, Position = 13, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public bool AutoIncrementLastFileVersionNumber { get; set; }
        /// <summary>
        /// Default false, will increment last segment of the existing version number by one.
        /// </summary>
        [Parameter(Mandatory = false, Position = 14, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public bool AutoIncrementLastVersionNumber { get; set; }


        private AssemblyInfo.AssemblyInfo task;

        public Update_AssemblyInfo()
        {
            task = new AssemblyInfo.AssemblyInfo(new PowershellCmdletLogger(this));
            AddIfMissing = task.AddIfMissing;
            AutoIncrementLastFileVersionNumber = task.AutoIncrementLastFileVersionNumber;
            AutoIncrementLastVersionNumber = task.AutoIncrementLastVersionNumber;
            AutoUpdateCopyrightYear = task.AutoUpdateCopyrightYear;
        }

        protected override void BeginProcessing()
        {
            task = new AssemblyInfo.AssemblyInfo(new PowershellCmdletLogger(this))
                .CopyPropertyValuesFrom(this) as AssemblyInfo.AssemblyInfo;
        }

        protected override void ProcessRecord()
        {
            task.Execute();
        }
    }
}
